<template>
  <div class="mod-config" >
    <div v-if="bookmanages">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item>
          <el-select v-model="dataForm.msgScope" placeholder="发送范围">
            <el-option v-for="item in options" :key="item.val" :label="item.label" :value="item.val"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="dataForm.pushState" placeholder="发送状态">
            <el-option v-for="item in options1" :key="item.val" :label="item.label" :value="item.val"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.msgName" placeholder="消息名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="pushTime"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="发送日期起"
            end-placeholder="发送日期止">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
          <el-button type="primary" @click="showRetailChange()">新增</el-button>
          <el-button
            type="danger"
            @click="deleteHandle()"
            :disabled="dataListSelections.length <= 0"
          >批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="msgName" header-align="center" align="center" label="消息名称"></el-table-column>
        <el-table-column prop="msgContext" header-align="center" align="center" label="消息内容"></el-table-column>
        <el-table-column prop="jumpLink" header-align="center" align="center" label="跳转链接"></el-table-column>
        <el-table-column prop="msgScope" header-align="center" align="center" label="发送范围">
          <template slot-scope="scope">
            <span v-if="scope.row.msgScope === 0">全部用户</span>
            <span v-if="scope.row.msgScope === 1">非会员</span>
            <span v-if="scope.row.msgScope === 2">注册会员</span>
          </template>
        </el-table-column>
        <el-table-column prop="pushTime" header-align="center" align="center" label="发送时间"></el-table-column>
        <el-table-column prop="pushState" header-align="center" align="center" label="发送状态">
          <template slot-scope="scope">
            <span v-if="scope.row.pushState === 0">未发</span>
            <span v-if="scope.row.pushState === 1">已发</span>
          </template>
        </el-table-column>
        <el-table-column prop="pushNum" header-align="center" align="center" label="推送人数"></el-table-column>
        <el-table-column prop="readNum" header-align="center" align="center" label="已读人数"></el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showRetailChange(scope.row.id)" v-if="scope.row.pushState !== 1">修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="dataForm.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="dataForm.limit"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
    </div>
    <!--查看详情-->
    <message-add ref="appMess" @updatTable="getDataList"></message-add>
  </div>
</template>

<script>
  import MessageAdd from './messageAdd'
  export default {
    data() {
      return {
        retailInfo: false,
        userGrade: '',
        options: [{
          val: null,
          label: "全部"
        },
          {
            val: '0',
            label: "全部用户"
          },
        {
          val: "2",
          label: "会员"
        },
        {
          val: "1",
          label: "非会员"
        }],
        options1: [{
          val: null,
          label: "全部"
        },{
          val: "1",
          label: "已发"
        },
        {
          val: "0",
          label: "未发"
        }],
        dataForm: {
          page: 1,
          limit: 10,
          msgType: 1
        },
        pushTime: [],
        userId: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        // 上架
        shelfVisible: false,
        viewDetail:false,
        isAlive: "",
        bookmanages:true,
        bookchapters:false,//章节
      };
    },
    mounted() {
      this.getDataList();
    },
    components: {
      MessageAdd
    },
    methods: {
      search(){
        this.dataForm.page = 1;
        this.getDataList();
      },
      // 获取数据列表
      getDataList () {
        if (this.dataForm.msgScope === null) {
          delete this.dataForm.msgScope
        }
        if (this.dataForm.pushState === null) {
          delete this.dataForm.pushState
        }
        if (this.pushTime.length > 0) {
          this.dataForm.pushStartTime = this.pushTime[0]
          this.dataForm.pushEndTime = this.pushTime[1]
        }
        this.dataListLoading = true;
        this.$http({
          url: this.$http.adornUrl("/user/msgtemplate/list"),
          method: "get",
          params: this.$http.adornParams(this.dataForm)
        }).then(({ data }) => {
          console.log(data, 999)
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      },
      deleteHandle(id) {
        var ids = id
          ? [id]
          : this.dataListSelections.map(item => {
            return item.id;
          });
        this.$confirm(
          `确定进行[${id ? "删除" : "批量删除"}]操作?`,
          // `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        ).then(() => {
          this.$http({
            url: this.$http.adornUrl("/user/msgtemplate/delete"),
            method: "post",
            data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      },
      showRetailChange (id) {
        if (id) {
          this.$refs.appMess.initData(id)
        } else {
          this.$refs.appMess.initData()
        }
      },
      batchAddView (id) {
        console.log(id)
      },
      showInfo (id) {
        this.userId = id
        this.bookmanages = false
        this.retailInfo = true
      },
      hideInfo () {
        this.bookmanages = true
        this.retailInfo = false
      },
      // 每页数
      sizeChangeHandle (val) {
        this.dataForm.limit = val;
        this.dataForm.page = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.dataForm.page = val;
        this.getDataList();
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val;
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      },
      detailView(id){
        this.viewDetail = true;
        this.$nextTick(() => {
          this.$refs.detail.init(id);
        });
      },
      getBookType(list) {
        if (list == null) {
          return;
        } else {
          let arr = [];
          let len = list.length;
          for (let i = 0; i < len; i++) {
            arr.push(list[i].categoryName);
          }
          return arr.join("、");
        }
      }
    }
  };
</script>
